//
//  DataBase_Movie.m
//  豆瓣
//
//  Created by dllo on 16/1/3.
//  Copyright © 2016年 HaiTeng. All rights reserved.
//

#import "DataBase_Movie.h"
#import <sqlite3.h>

@implementation DataBase_Movie



+ (DataBase_Movie *)shareDataBaseHandle{
    
    static DataBase_Movie *dataBase_movie = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
        dataBase_movie = [[DataBase_Movie alloc] init];
        
    });
    return dataBase_movie;
    
}

static sqlite3 *db_movie = nil;
//打开数据库
- (void)openDB{
 
    
    //如果，没被打开就打开
    if (db_movie == nil) {
        //取到沙盒地址
        NSString *tempPath = NSTemporaryDirectory();
        NSLog(@"%@",tempPath);
        NSString *filePath = [tempPath stringByAppendingString:@"movie.sqlite"];
        
        
        //在沙盒中去操作,打开路径下的数据库
        int result = sqlite3_open(filePath.UTF8String, &db_movie);
        if (result == SQLITE_OK) {
            NSLog(@"打开数据库，成功");
            //写sql创建语句(创建表格)
            NSString *sql = @"CREATE  TABLE  IF NOT EXISTS MovieModel (title TEXT PRIMARY KEY  NOT NULL , plot_simple TEXT, genres TEXT, country TEXT,runtime TEXT, poster TEXT, rating_count TEXT, rating TEXT, release_date TEXT, actors TEXT)";
            //执行sql语句
            int result2 = sqlite3_exec(db_movie, sql.UTF8String, NULL, NULL, NULL);
            if (result2 == SQLITE_OK) {
                NSLog(@"创建表格成功");
            }else{
                NSLog(@"创建表格失败");
            }
            
        }else{
            NSLog(@"打开数据库失败");
        }
        
        
    }
    

    
    
}

//关闭数据库
- (void)closeDB{
    
    //如果数据库不为空，关闭它
    if (db_movie != nil) {
        int result = sqlite3_close(db_movie);
        if (result == SQLITE_OK) {
            NSLog(@"关闭数据库成功");
            db_movie = nil; //同时把数据库对象置空
        }else{
            NSLog(@"关闭数据库失败");
        }
        
    }
    
    
    
}

//添加数据
- (void)insertMovie:(MovieModel *)movie{
    
    
    //写sql添加语句
    NSString *sql = [NSString stringWithFormat:@"INSERT INTO MovieModel (title,plot_simple,genres,country,runtime,poster,rating_count,rating,release_date,actors) VALUES ('%@','%@','%@','%@','%@','%@','%@','%@','%@','%@')",movie.title,movie.plot_simple,movie.genres,movie.country,movie.runtime,movie.poster,movie.rating_count,movie.rating,movie.release_date,movie.actors];
    //执行sql语句
    int result = sqlite3_exec(db_movie, sql.UTF8String, NULL, NULL, NULL);
    if (result == SQLITE_OK) {
        NSLog(@"添加数据成功");
    }else{
        NSLog(@"添加数据失败");
    }
    
    
}

//删除数据
- (void)deleteMovie:(NSString *)movie{
    
    //写sql删除语句
    NSString *sql = [NSString stringWithFormat:@"DELETE FROM MovieModel WHERE title = '%@'",movie];
    //执行sql语句
    int result = sqlite3_exec(db_movie, sql.UTF8String, NULL, NULL, NULL);
    if (result == SQLITE_OK) {
        NSLog(@"删除成功");
    }else{
        NSLog(@"删除失败");
    }
    
    
    
}


//更新数据
- (void)updataMovie:(MovieModel *)movie{
    
    
    NSString *sql = [NSString stringWithFormat:@"UPDATE MovieModel SET plot_simple = '%@' , genres = '%@',country = '%@',runtime = '%@',poster = '%@',rating_count = '%@',rating = '%@',release_date = '%@',actors = '%@' WHERE title = '%@'",movie.plot_simple,movie.genres,movie.country,movie.runtime,movie.poster,movie.rating_count,movie.rating,movie.release_date,movie.actors,movie.title];
    int resulet = sqlite3_exec(db_movie, sql.UTF8String, NULL, NULL, NULL);
    if (resulet == SQLITE_OK) {
        NSLog(@"更新数据成功");
    }else{
        NSLog(@"更新数据失败");
    }
    
    
}

//查找数据
- (NSMutableArray *)selectmovie{
   
    
    NSMutableArray *arrayAllStudents = [NSMutableArray array];
    
    NSString *sql = @"SELECT *FROM MovieModel";
    //（第三个参数我们写-1自动计算sql的长度）
    //(第四个参数叫: 游标)
    sqlite3_stmt *stmt_ = nil;
    int result = sqlite3_prepare_v2(db_movie, sql.UTF8String, -1, &stmt_, NULL);
    if (result == SQLITE_OK) {
        NSLog(@"执行sql成功，开始找数据");
        //一行一行取查找数据，直到找完为止
        while (sqlite3_step(stmt_) == SQLITE_ROW) {
            //在这里把它一个一个的数据拿出来
            //(找title 数据)
            const char *title1 = (const char *)sqlite3_column_text(stmt_, 0);
            NSString *title = [NSString stringWithCString:title1 encoding:NSUTF8StringEncoding];
            
            //(找plot_simple 数据)
            const char *plot_simple1 = (const char *)sqlite3_column_text(stmt_, 1);
            NSString *plot_simple = [NSString stringWithCString:plot_simple1 encoding:NSUTF8StringEncoding];
            
            //(找genres 数据)
            const char *genres1 = (const char *)sqlite3_column_text(stmt_, 2);
            NSString *genres = [NSString stringWithCString:genres1 encoding:NSUTF8StringEncoding];
            
            //(找country 数据)
            const char *country1 = (const char *)sqlite3_column_text(stmt_, 3);
            NSString *country = [NSString stringWithCString:country1 encoding:NSUTF8StringEncoding];
            
            //(找runtime 数据)
            const char *runtime1 = (const char *)sqlite3_column_text(stmt_, 4);
            NSString *runtime = [NSString stringWithCString:runtime1 encoding:NSUTF8StringEncoding];
            
            //(找poster 数据)
            const char *poster1 = (const char *)sqlite3_column_text(stmt_, 5);
            NSString *poster = [NSString stringWithCString:poster1 encoding:NSUTF8StringEncoding];
            
            //(找rating_count 数据)
            const char *rating_count1 = (const char *)sqlite3_column_text(stmt_, 6);
            NSString *rating_count = [NSString stringWithCString:rating_count1 encoding:NSUTF8StringEncoding];
            
            //(找rating 数据)
            const char *rating1 = (const char *)sqlite3_column_text(stmt_, 7);
            NSString *rating = [NSString stringWithCString:rating1 encoding:NSUTF8StringEncoding];
            
            //(找release_date 数据)
            const char *release_date1 = (const char *)sqlite3_column_text(stmt_, 8);
            NSString *release_date = [NSString stringWithCString:release_date1 encoding:NSUTF8StringEncoding];
            
            //(找actors 数据)
            const char *actors1 = (const char *)sqlite3_column_text(stmt_, 9);
            NSString *actors = [NSString stringWithCString:actors1 encoding:NSUTF8StringEncoding];
            
            
            //            //把取到的数据放到数据模型中
            
            MovieModel *model = [[MovieModel alloc] init];
            model.title = title;
            model.plot_simple = plot_simple;
            model.genres = genres;
            model.country = country;
            model.runtime = runtime;
            model.poster = poster;
            model.rating_count = rating_count;
            model.rating = rating;
            model.release_date = release_date;
            model.actors = actors;
            
            [arrayAllStudents addObject:model];
        }
        
        
        
        
        
    }else{
        NSLog(@"执行sql失败");
        
    }
    //释放游标
    sqlite3_finalize(stmt_);
    
    return arrayAllStudents;
    
    
}



@end
